package net.wesley.android.city;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import android.os.Handler;
import android.os.Message;

import net.wesley.android.City;
import net.yebaihe.sdk.HttpConnection;

public class HaErBin extends City {

	private Handler request1handler=new Handler(){
		public void handleMessage(Message message){
			String[] rows;
			boolean firstrow;
			String retvalue;
			int total;
			switch (message.what){
			case HttpConnection.DID_ERROR://DID ERROR
				callback.onComplete(-1, "");
				break;
			case HttpConnection.DID_SUCCEED://DID SUCC
				String str=(String)message.obj;
		        if (str.indexOf("发布时间")<=0){
		        	callback.onComplete(0, "");
		        	return;
		        }
		        str=str.split("发布时间")[1];
		        str=str.split("</table>")[0];
		        rows=str.split("<tr");
		        firstrow=true;
		        retvalue="";
		        total=0;
		        for (int i=0;i<rows.length;i++){
		            if (firstrow){
		                firstrow=false;
		                continue;
		            }
		            String row=rows[i];
		            String[] cols = row.split("<td");
		            if (cols.length<4){
		                continue;
		            }
		            String shijian = cols[1].split(">")[2].split("<")[0].trim().replace("&nbsp;","");
		            if (shijian.trim().length()<=0){
		                continue;
		            }
		            String didian = cols[2].split(">")[2].split("<")[0].trim().replace("&nbsp;","");
		            String code = cols[3].split(">")[2].split("<")[0].trim().replace("&nbsp;","");
		            retvalue+=String.format("shijian:%s\ndidian:%s\ncode:%s\n\n",shijian,didian,code);
		            total+=1;
		        }
		        callback.onComplete(total, retvalue);
				break;
			}
		}
	};

	public void requestParamsFinish(String[] values) {
		try {
			new HttpConnection(ctx,request1handler,"GBK").post("http://www.hrbjtwf.com/wzxxx4.ASP", 
					String.format("cllx=02&keyword=%s&submit=%s",
							chepai.substring(1),URLEncoder.encode("提交","GBK")));
		} catch (UnsupportedEncodingException e) {
			callback.onComplete(-10001, "");
			e.printStackTrace();
		}
	}

	@Override
	public boolean match(String chepai) {
		return chepai.startsWith("黑A");
	}

	@Override
	public String[] paramDesc() {
		return null;
	}

}
